<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/28
 * Time: 10:52
 */
namespace app\api\controller;
use think\Controller;
class Server extends Controller
{
    public $ser = null;
    public $url = null;
    public $res = null;

    public function __construct()
    {
        parent::__construct();

        $this->ser = self::hproseServer();
        $a = $this->getMethods(__CLASS__);
        foreach($a as $v){
            if($v !='__construct' && $v!='__destruct'){
                $this->ser->addMethod($v, $this);
            }
        }
        $this->ser->start();
    }

  /*  protected function exe($ar=[]){
       $this->ser = self::hproseServer();
        foreach($ar as $v){
            if($v !='__construct' && $v!='__destruct'){
                $this->ser->addMethod($v, $this);
            }
        }
        $this->ser->start();
    }*/

    public function hello($word = '')
    {
        echo   '你好--' . $word;die;
    }

    public function test($word = '')
    {
        return "这是测试数据--" . $word;
    }

    public function __destruct()
    {
        $this->ser = null;
    }


    public function getMethods($classname, $access = null)
    {
        $class = new \ReflectionClass($classname);
        $methods = $class->getMethods();
        $returnArr = array();
        foreach ($methods as $value) {
            if ($value->class == $classname) {
                if ($access != null) {
                    $methodAccess = new \ReflectionMethod($classname, $value->name);
                    switch ($access) {
                        case 'public':
                            if ($methodAccess->isPublic()) $returnArr[$value->name] = 'public';
                            break;
                        case 'protected':
                            if ($methodAccess->isProtected()) $returnArr[$value->name] = 'protected';
                            break;
                        case 'private':
                            if ($methodAccess->isPrivate()) $returnArr[$value->name] = 'private';
                            break;
                        case 'final':
                            if ($methodAccess->isFinal()) $returnArr[$value->name] = 'final';
                            break;
                    }
                } else {
                    array_push($returnArr, $value->name);
                }

            }
        }
        return $returnArr;
    }

    /**
     * @param $classname
     * @param null $access
     * @return array
     * 只获取本类的方法名，不包括父级方法名，
     */
  /*  public function getMethods($classname,$class_ar=[], $access = null)
    {
        if($class_ar && is_array($class_ar)){
            $class_ar=array_merge([__CLASS__],$class_ar);
        }
        $class = new \ReflectionClass($classname);
        $methods = $class->getMethods();

        $returnArr = array();
        foreach ($methods as $value) {
            if($class_ar && is_array($class_ar)) {
                if (in_array($value->class, $class_ar)) {
                    if ($access != null) {
                        $methodAccess = new \ReflectionMethod($classname, $value->name);
                        switch ($access) {
                            case 'public':
                                if ($methodAccess->isPublic()) $returnArr[$value->name] = 'public';
                                break;
                            case 'protected':
                                if ($methodAccess->isProtected()) $returnArr[$value->name] = 'protected';
                                break;
                            case 'private':
                                if ($methodAccess->isPrivate()) $returnArr[$value->name] = 'private';
                                break;
                            case 'final':
                                if ($methodAccess->isFinal()) $returnArr[$value->name] = 'final';
                                break;
                        }
                    } else {
                        array_push($returnArr, $value->name);
                    }
                }
            }elseif($value->class == $classname) {
                if ($access != null) {
                    $methodAccess = new \ReflectionMethod($classname, $value->name);
                    switch ($access) {
                        case 'public':
                            if ($methodAccess->isPublic()) $returnArr[$value->name] = 'public';
                            break;
                        case 'protected':
                            if ($methodAccess->isProtected()) $returnArr[$value->name] = 'protected';
                            break;
                        case 'private':
                            if ($methodAccess->isPrivate()) $returnArr[$value->name] = 'private';
                            break;
                        case 'final':
                            if ($methodAccess->isFinal()) $returnArr[$value->name] = 'final';
                            break;
                    }
                } else {
                    array_push($returnArr, $value->name);
                }
            }
        }
        return $returnArr;
    }*/

    public static function hproseServer(){
        return  new \Hprose\Http\Server();
    }

    public static function hproseClient($url,$p=false){
        $client= \Hprose\Client::create($url,$p);
        return $client;
    }

}